<?php

namespace App\Console\Commands;

use App\Models\Base\LogDropLevel;
use App\Service\PlayerService;
use App\Service\ServerService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class FetchLevelRemain extends CommandBase
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch:levelRemain';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '用户留存等级统计';

    protected $model;

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            //区服统计
            $this->serverStat();
            //全平台统计
            $this->performStatistical();
        } catch (\Exception $e) {
            Log::error('统计等级留存错误'.$e->getMessage());
            dd($e);
        }
    }

    /**
     * 全平台统计
     */
    protected function performStatistical()
    {
        $levelData = LogDropLevel::where('type', 3)
            ->selectRaw('level,sum(role_number) as role_number,avg(wastage_rate) as wastage_rate,avg(level_proportion) as level_proportion, 
            avg(total_level_proportion) as total_level_proportion,sum(three_not_online_number) as three_not_online_number,
            avg(three_wastage_rate) as three_wastage_rate,sum(one_day_not_online) as one_day_not_online,avg(one_day_wastage_rate) as one_day_wastage_rate')
            ->groupBy('level')->get();
        if ($levelData->isEmpty()) {
            return false;
        }

        foreach ($levelData as $level) {
            $model = LogDropLevel::where('level', $level->level)->where('type', self::STAT_TYPE_PLATFORM)->first();
            if (null == $model) {
                $model = new LogDropLevel();
            }
            $model->type = self::STAT_TYPE_PLATFORM;
            $model->level = $level->level;
            $model->role_number = $level->role_number;
            //等级流失率
            $model->wastage_rate = $level->wastage_rate;
            //等级比重
            $model->level_proportion = $level->level_proportion;
            //累积等级比重
            $model->total_level_proportion = $level->total_level_proportion;
            //三天未登录数量
            $model->three_not_online_number = $level->three_not_online_number;
            //三日流失率
            $model->three_wastage_rate = $level->three_wastage_rate;
            //24小时流失率
            $model->one_day_wastage_rate = $level->one_day_wastage_rate;
            $model->save();
        }
    }

    /**
     * 区服统计.
     */
    protected function serverStat()
    {
        $serverIds = ServerService::serverIds();
        if (!$serverIds) {
            return false;
        }
        foreach ($serverIds as $serverId) {
            setDB($serverId);
            $level_data = PlayerService::playerLevelNumber();
            //总人数
            $role_number = $level_data->sum('level_number');
            foreach ($level_data as $level) {
                $model = LogDropLevel::where('level', $level->level)->where('server_id', $serverId)->first();
                if (null == $model) {
                    $model = new LogDropLevel();
                }
                $model->type = self::STAT_TYPE_SERVER;
                $model->server_id = $serverId;
                $model->level = $level->level;
                $model->role_number = $level->level_number;
                $other_max_number = thanLevel($level_data->toArray(), $level->level);
                $other_min_number = lessLevel($level_data->toArray(), $level->level);
                //等级流失率
                $model->wastage_rate = $level->level_number / $other_max_number;
                //等级比重
                $model->level_proportion = $level->level_number / $role_number;
                //累积等级比重
                $model->total_level_proportion = $other_min_number / $role_number;
                //三天未登录数量
                $three_day = Carbon::parse('3 days ago')->timestamp;
                $threeDay_notLogin_number = PlayerService::somedayNotLogin($level->level, $three_day)->count();
                $model->three_not_online_number = $threeDay_notLogin_number;
                //三日流失率
                $model->three_wastage_rate = $threeDay_notLogin_number / $other_max_number;
                //24时未在线人数
                $one_day_not_online = PlayerService::somedayNotLogin(
                    $level->level, $this->yesterdayTimestamp)->count();
                $model->one_day_not_online = $one_day_not_online;
                //24小时流失率
                $model->one_day_wastage_rate = $one_day_not_online / $other_max_number;
                $model->save();
            }
        }
    }
}
